package com.llkj.qingnianguoxue.zhuanlan;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.GiftAdapter;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.bean.ZhiBoGiftBean;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GridSpacingItemDecoration;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 直播礼物
 */
public class GiftFragment extends BaseFragment {
    private OnFragmentInteractionListener mListener;
    public static final String GIFT_DATA = "gift_data";
    @BindView(R.id.giftGridView)
    protected RecyclerView mGiftGridView;
    private GiftAdapter mGiftAdapter;//礼物适配器
    private List<ZhiBoGiftBean> mGiftData;

    /**
     * 直播礼物Fragment初始化
     * @param giftList Parameter 1.
     * @return A new instance of fragment GiftFragment.
     */
    public static GiftFragment newInstance(List<ZhiBoGiftBean> giftList) {
        GiftFragment fragment = new GiftFragment();
        Bundle args = new Bundle();
        args.putSerializable(GIFT_DATA, (Serializable) giftList);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mGiftData = (List<ZhiBoGiftBean>) getArguments().getSerializable(GIFT_DATA);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_gift, container, false);
        ButterKnife.bind(this,rootView);
        initView();
        return rootView;
    }

    private void initView(){
        mGiftGridView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mGiftGridView.addItemDecoration(new GridSpacingItemDecoration(3, DisplayUtil.dip2px(getContext(),5), false));
        mGiftAdapter = new GiftAdapter(R.layout.item_gift,mGiftData);
        mGiftGridView.setAdapter(mGiftAdapter);

        mGiftGridView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if(mListener != null){
                    mGiftAdapter.setSelectGift(i);
                    ZhiBoGiftBean giftBean = mGiftData.get(i);
                    mListener.selectGift("1",giftBean.getPrice());
                }
            }
        });
    }

    public void setOnFragmentListener(OnFragmentInteractionListener listener){
        mListener = listener;
    }

    /**
     * 回调接口
     */
    public interface OnFragmentInteractionListener {
        void selectGift(String giftId, String price);
    }
}
